﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace iSmartCreatorApp
{
    /// <summary>
    /// Interaction logic for NewFile.xaml
    /// </summary>
    public partial class NewFile : Window
    {
        public NewFile()
        {
            InitializeComponent();
            BtnAdd.IsEnabled = false;
            TxtBxFileName.Focus();
        }

        //TextBox with filename changed
        private void TxtBxFileName_TextChanged(object sender, TextChangedEventArgs e)
        {
            BtnAdd.IsEnabled = TxtBxFileName.Text != "";
        }

        //Button "Add" click
        private void BtnAdd_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var mainWindow = Owner as MainWindow; //Making reference to MainForm as owner
                var paragraph = new Paragraph();
                var flow = new FlowDocument();

                NewDocument nd = new NewDocument();
                NewDocument.pageDTD =
                    "!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\"";
                NewDocument.pageHtmlType = "";

                MainWindow.dictionary["$DTD"] = NewDocument.pageDTD;
                MainWindow.dictionary["$html"] = NewDocument.pageHtmlType;
                MainWindow.fileName = TxtBxFileName.Text + ".html";
                nd.CreateNewFile(paragraph, flow, mainWindow);
            }
            catch (ArgumentException)
            {
                MessageBox.Show("Invalid file name!", "Error", MessageBoxButton.OK);
            }
            this.Close();
        }
    }
}
